<?php
defined("IN_IA") or exit("Access Denied");
function tocurl($url = '', $data = '', $timeout = 0)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    if ($timeout > 0) {
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if ($httpcode == 200) {
        return $output;
    } else {
        return false;
    }
}
function getaccess_token()
{
    global $_W;
    $system = pdo_get("yzzc_sun_system", array("uniacid" => $_W["uniacid"]), array("appid", "appsecret"));
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $system["appid"] . "&secret=" . $system["appsecret"];
    $output = tocurl($url, '', 0);
    $info = json_decode($output, true);
    $token = $info["access_token"];
    return $token;
}
function sendtelmessage($openid = '', $tpltype = '', $haveformid = '', $data = '')
{
    global $_W;
    if (empty($openid)) {
        return false;
    }
    if (empty($haveformid)) {
        $delres = pdo_delete("yzzc_sun_userformid", array("time <=" => date("Y-m-d", strtotime("-7 days")), "uniacid" => $_W["uniacid"]));
        $delres = pdo_delete("yzzc_sun_userformid", array("form_id like" => "mock", "uniacid" => $_W["uniacid"]));
        $now = date("Y-m-d", strtotime("-7 days"));
        $sql = "select form_id from " . tablename("yzzc_sun_userformid") . " where openid='" . $openid . "' and time>='" . $now . "' order by id asc";
        $form_id = pdo_fetchcolumn($sql);
        $delres = pdo_delete("yzzc_sun_userformid", array("form_id" => $form_id, "uniacid" => $_W["uniacid"]));
    } else {
        $form_id = $haveformid;
    }
    $d_set = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $_W["uniacid"]), array("tpl_wd_arrival", "tpl_wd_fail", "tpl_share_check"));
    if (!empty($form_id)) {
        $tpl = $d_set[$tpltype];
        if (empty($tpl)) {
            return false;
        }
        switch ($tpltype) {
            case "tpl_wd_arrival":
                $formwork = "{\r\n\"touser\": \"" . $openid . "\",\r\n\"template_id\": \"" . $tpl . "\",\r\n\"page\":\"yzzc_sun/pages/index/index\",\r\n\"form_id\":\"" . $form_id . "\",\r\n\"data\": {\r\n                        \"keyword1\": {\r\n                            \"value\": \"" . $data["price"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword2\": {\r\n                            \"value\":\"" . $data["ratesmoney"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword3\": {\r\n                            \"value\": \"" . $data["realmoney"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword4\": {\r\n                            \"value\":\"已到账\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword5\": {\r\n                            \"value\": \"" . $data["wd_type"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        }\r\n                    }   \r\n                }";
                break;
            case "tpl_wd_fail":
                $formwork = "{\r\n\"touser\": \"" . $openid . "\",\r\n\"template_id\": \"" . $tpl . "\",\r\n\"page\":\"yzzc_sun/pages/index/index\",\r\n\"form_id\":\"" . $form_id . "\",\r\n\"data\": {\r\n                        \"keyword1\": {\r\n                            \"value\": \"" . $data["price"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword2\": {\r\n                            \"value\":\"提现失败，请联系小程序运营商\",\r\n                            \"color\": \"#173177\"\r\n                        }\r\n                    }   \r\n                }";
                break;
            case "tpl_share_check":
                $formwork = "{\r\n\"touser\": \"" . $openid . "\",\r\n\"template_id\": \"" . $tpl . "\",\r\n\"page\":\"yzzc_sun/pages/index/index\",\r\n\"form_id\":\"" . $form_id . "\",\r\n\"data\": {\r\n                        \"keyword1\": {\r\n                            \"value\": \"" . $data["status"] . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                        \"keyword2\": {\r\n                            \"value\":\"" . date("Y-m-d H:i:s") . "\",\r\n                            \"color\": \"#173177\"\r\n                        },\r\n                         \"keyword3\": {\r\n                            \"value\":\"分销商审核\",\r\n                            \"color\": \"#173177\"\r\n                        }\r\n                    }   \r\n                }";
                break;
            default:
        }
        $access_token = getaccess_token();
        if (!empty($formwork) && !empty($access_token)) {
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token . '';
            tocurl($url, $formwork, 0);
        }
    }
}